﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace suirui.ZhuMu.Common
{
    public class DESUtils
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="stringToEncrypt"></param>
        /// <returns></returns>
        public static string EncryptString(string stringToEncrypt)
        {
            string str;
            if (string.IsNullOrEmpty(stringToEncrypt))
            {
                return "";
            }
            string empty = string.Empty;
            byte[] bytes = Encoding.UTF8.GetBytes(stringToEncrypt);
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, GenerateDESCryptoServiceProvider().CreateEncryptor(), CryptoStreamMode.Write);
            try
            {
                try
                {
                    cryptoStream.Write(bytes, 0, (int)bytes.Length);
                    cryptoStream.FlushFinalBlock();
                    empty = Convert.ToBase64String(memoryStream.ToArray(), 0, (int)memoryStream.Length);
                    return empty;
                }
                catch
                {
                    str = "";
                }
            }
            finally
            {
                memoryStream.Close();
                cryptoStream.Close();
            }
            return str;
        }

        /// <summary>
        /// 通过密钥解密
        /// </summary>
        /// <param name="stringToDecrypt"></param>
        /// <returns></returns>
        public static string DecryptString(string stringToDecrypt)
        {
            string stringDecrypted = string.Empty;
            if (String.IsNullOrEmpty(stringToDecrypt))
            {
                return "";
            }
            try
            {
                byte[] bytIn = Convert.FromBase64String(stringToDecrypt.Replace(" ", "+"));
                MemoryStream decryptStream = new MemoryStream();
                CryptoStream encStream = new CryptoStream(decryptStream, GenerateDESCryptoServiceProvider().CreateDecryptor(), CryptoStreamMode.Write);
                encStream.Write(bytIn, 0, bytIn.Length);
                encStream.FlushFinalBlock();
                stringDecrypted = UTF8Encoding.UTF8.GetString(decryptStream.ToArray());
                decryptStream.Close();
                encStream.Close();
            }
            catch
            {
                return "";
            }
            return stringDecrypted;
            //return stringToDecrypt;
        }

        private static DESCryptoServiceProvider GenerateDESCryptoServiceProvider()
        {
            string str;
            DESCryptoServiceProvider dESCryptoServiceProvider = new DESCryptoServiceProvider();
            string str1 = AppConfigration.Configuration["Encryptionkey"];
            if (dESCryptoServiceProvider.LegalKeySizes.Length == 0)
            {
                str = str1;
            }
            else
            {
                int minSize = dESCryptoServiceProvider.LegalKeySizes[0].MinSize;
                while (str1.Length > 7)
                {
                    str1 = str1.Substring(0, 7);
                }
                str = str1.PadRight(minSize / 8, ' ');
            }
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            dESCryptoServiceProvider.Key = bytes;
            dESCryptoServiceProvider.IV = bytes;
            return dESCryptoServiceProvider;
        }
    }
}
